/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionbean;

import entitybean.Albums;
import entitybean.Artists;
import entitybean.Country;
import entitybean.Genres;
import entitybean.Musicians;
import entitybean.Producers;
import entitybean.Songs;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

/**
 *
 * @author ShahanThai
 */
@Stateless
@RemoteBinding(jndiBinding = "SongsJNDI")
@LocalBinding(jndiBinding = "SongsLocalJNDI")
public class SongsSessionBean implements SongsSessionBeanRemote, SongsSessionBeanLocal {

    @PersistenceContext(unitName = "J2EE_ShareMusic-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public List getSong(int songID) {
        Songs song = em.find(Songs.class, songID);
        List list = null;
        if (song != null) {
            list = new ArrayList();
            list.add(song);
        }
        return list;
    }

    public List getSong(String searchValue, String findBy, String category) {
        List result = new ArrayList();
        if (findBy.equals("") || findBy.equals("*")) {
            if (!category.equals("")) {
                String jpql = "SELECT a FROM Songs a WHERE a.name LIKE :search and a.isSong = :isSong ";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                if (category.equals("video")) {
                    query.setParameter("isSong", false);
                } else if (category.equals("music")) {
                    query.setParameter("isSong", true);
                }
                result = query.getResultList();
            } else {
                String jpql = "SELECT a FROM Songs a WHERE a.name LIKE :search";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                result = query.getResultList();
            }
        } else if (findBy.equals("artist")) {
            if (!category.equals("")) {
                String jpql = "SELECT a FROM Songs a WHERE a.artists.artistName LIKE :search and a.isSong = :isSong ";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                if (category.equals("video")) {
                    query.setParameter("isSong", false);
                } else if (category.equals("music")) {
                    query.setParameter("isSong", true);
                }
                result = query.getResultList();
            } else {
                String jpql = "SELECT a FROM Songs a WHERE a.artists.artistName LIKE :search";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                result = query.getResultList();
            }
        } else if (findBy.equals("composer")) {
            if (!category.equals("")) {
                String jpql = "SELECT a FROM Songs a WHERE a.musicians.musicianName LIKE :search and a.isSong = :isSong ";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                if (category.equals("video")) {
                    query.setParameter("isSong", false);
                } else if (category.equals("music")) {
                    query.setParameter("isSong", true);
                }
                result = query.getResultList();
            } else {
                String jpql = "SELECT a FROM Songs a WHERE a.musicians.musicianName LIKE :search";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                result = query.getResultList();
            }
        } else if (findBy.equals("album")) {
            if (!category.equals("")) {
                String jpql = "SELECT a FROM Songs a WHERE a.albums.albumName LIKE :search and a.isSong = :isSong ";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                if (category.equals("video")) {
                    query.setParameter("isSong", false);
                } else if (category.equals("music")) {
                    query.setParameter("isSong", true);
                }
                result = query.getResultList();
            } else {
                String jpql = "SELECT a FROM Songs a WHERE a.albums.albumName LIKE :search";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                result = query.getResultList();
            }
        } else if (findBy.equals("lyric")) {
            if (!category.equals("")) {
                String jpql = "SELECT a FROM Songs a WHERE a.lyrics LIKE :search and a.isSong = :isSong ";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                if (category.equals("video")) {
                    query.setParameter("isSong", false);
                } else if (category.equals("music")) {
                    query.setParameter("isSong", true);
                }
                result = query.getResultList();
            } else {
                String jpql = "SELECT a FROM Songs a WHERE a.lyrics LIKE :search";
                Query query = em.createQuery(jpql);
                query.setParameter("search", "%" + searchValue + "%");
                result = query.getResultList();
            }
        }
        return result;
    }

    public List getUploader(int songID) {
        String jpql = "SELECT a FROM Upload a WHERE a.songs.songID= :id";
        Query query = em.createQuery(jpql);
        query.setParameter("id", songID);
        List result = query.getResultList();

        return result;
    }

    public List getSongSameArtist(int id) {
        String jpql = "SELECT a FROM Songs a WHERE a.artists.artistID= :id AND a.songID != :id";
        Query query = em.createQuery(jpql);
        query.setParameter("id", id);
        query.setMaxResults(10);
        List result = query.getResultList();
        return result;
    }

    public List getSongSameGenre(int id) {
        List list = null;
        Songs song = em.find(Songs.class, id);
        String jpql = "SELECT a FROM Songs a WHERE a.genres.genreID= :id AND a.country.countryID = :countryID AND a.songID != :id";
        Query query = em.createQuery(jpql);
        query.setParameter("id", id);
        query.setParameter("countryID", song.getCountry().getCountryID());
        query.setMaxResults(10);
        List result = query.getResultList();
        return result;
    }

    public List getRelativeSong(int id) {
        List list = null;
        Songs song = em.find(Songs.class, id);
        String jpql = "SELECT a FROM Songs a WHERE a.name LIKE :name AND a.country.countryID = :countryID";
        Query query = em.createQuery(jpql);
        query.setParameter("name", "%" + song.getName() + "%");
        query.setParameter("countryID", song.getCountry().getCountryID());
        query.setMaxResults(10);
        List result = query.getResultList();
        return result;
    }

    public String getSongName(int id) {
        Songs song = em.find(Songs.class, id);
        if (song != null) {
            return song.getName();
        }
        return null;
    }

    public List getSong(String songName) {
        String jpql = "SELECT a FROM Songs a WHERE a.name LIKE :name";
        Query query = em.createQuery(jpql);
        query.setParameter("name", "%" + songName + "%");
        List result = query.getResultList();

        return result;
    }

    public List loadSong() {
        String jpql = "SELECT a FROM Songs a";
        javax.persistence.Query query = em.createQuery(jpql);
        List list = query.getResultList();
        return list;
    }

    public boolean deleteSong(int id) {
        String jpql = "DELETE FROM Upload a WHERE A.songs.songID =:id";
        javax.persistence.Query query = em.createQuery(jpql);
        query.setParameter("id", id);
        int result = query.executeUpdate();
        Songs song = em.find(Songs.class, id);
        if (song != null) {
            em.remove(song);
            return true;
        }
        return false;
    }

    public boolean addSong(String name, int artistID, int albumID, int genreID, int producerID, String lyric, int countryID, int musicianID, int year, int listened, int download, int no, boolean isSong, String l1, String l2, String l3, String l4, String d1, String d2, String d3, String d4, String thumbnail) {
        Artists artist = new Artists(artistID);
        Genres genre = new Genres(genreID);
        Albums album = new Albums(albumID);
        Producers producer = new Producers(producerID);
        Country country = new Country(countryID);
        Musicians musician = new Musicians(musicianID);
        Songs newA = new Songs(name, artist, album, genre, producer, lyric, country, musician, year, listened, download, no, isSong, l1, l2, l3, l4, d1, d2, d3, d4, thumbnail);
        persist(newA);
        return true;
    }

    public boolean saveSong(int id, String name, int artistID, int albumID, int genreID, int producerID, String lyric, int countryID, int musicianID, int year, int listened, int download, int no, boolean isSong, String l1, String l2, String l3, String l4, String d1, String d2, String d3, String d4, String thumbnail) {
        Songs song = em.find(Songs.class, id);
        if (song != null) {
            Artists artist = new Artists(artistID);
            Genres genre = new Genres(genreID);
            Albums album = new Albums(albumID);
            Producers producer = new Producers(producerID);
            Country country = new Country(countryID);
            Musicians musician = new Musicians(musicianID);
            song.setName(name);
            song.setArtists(artist);
            song.setAlbums(album);
            song.setGenres(genre);
            song.setProducers(producer);
            song.setLyrics(lyric);
            song.setCountry(country);
            song.setMusicians(musician);
            song.setPublishYear(year);
            song.setListened(listened);
            song.setDownloaded(download);
            song.setNo(no);
            song.setIsSong(isSong);
            song.setL1(l1);
            song.setL2(l2);
            song.setL3(l3);
            song.setL4(l4);
            song.setD1(d1);
            song.setD2(d2);
            song.setD3(d3);
            song.setD4(d4);
            song.setThumbnail(thumbnail);
            persist(song);
            return true;
        } else {
            return false;
        }
    }
}
